home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1997 / HAM Radio 1997.iso / vcls / glyphpro / main.pas < prev    next >
Pascal/Delphi Source File  |  1996-04-08  |  14KB  |  477 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Buttons, StdCtrls, ExtCtrls, Options;
  8. type
  9.   Direction = (Up, Down);
  10. type
  11.   TForm1 = class(TForm)
  12.     PanelToolBar: TPanel;
  13.     PanelClient: TPanel;
  14.     SpeedButtonPgDn: TSpeedButton;
  15.     ComboBoxDir: TComboBox;
  16.     Label1: TLabel;
  17.     SpeedButtonDisplay: TSpeedButton;
  18.     SpeedButtonOptions: TSpeedButton;
  19.     SpeedButton2: TSpeedButton;
  20.     SpeedButtonAbout: TSpeedButton;
  21.     procedure DisplayAll(Sender: TObject);
  22.     procedure BitBtnClearClick(Sender: TObject);
  23.     procedure SpeedBtnScrollDown(Sender: TObject);
  24.     procedure PanelClientResize(Sender: TObject);
  25.     procedure SpeedButtonOptionsClick(Sender: TObject);
  26.     procedure FormCreate(Sender: TObject);
  27.     procedure SpeedButtonAboutClick(Sender: TObject);
  28.     procedure ComboBoxDirChange(Sender: TObject);
  29.     procedure ComboBoxDirKeyDown(Sender: TObject; var Key: Word;
  30.       Shift: TShiftState);
  31.     procedure FormShow(Sender: TObject);
  32.     procedure FormDestroy(Sender: TObject);
  33.     procedure SpeedButton2Click(Sender: TObject);
  34.   private
  35.     { Private declarations }
  36.  
  37.     OptionsList:TStringList; { Saved in Glyph.Ini }
  38.     function  CreateNewSpeedBtn(location: TPoint; size: integer;
  39.                                 BmpFile: string) : boolean;
  40.     procedure Display;
  41.     procedure Initialize;
  42.     function  Position(OldPosition : TPoint): TPoint;
  43.     procedure FormAdjustSize;
  44.     function  NewPage(d: Direction; var SearchRec: TSearchRec): integer;
  45.     function  DirExists(const S : String) : boolean;
  46.     function  UpdateIniFile(const INIFileName: string;
  47.                             OptionsList: TStringList): boolean;
  48.   public
  49.     { Public declarations }
  50.     FormOptions : TFormOptions;
  51.  
  52.     { ini parameters }
  53.     iButSize:     byte; { buts size }
  54.     iNumOfGlyphs: integer;
  55.     iSetLimit:    integer;
  56.     sDelphiDir:   string;
  57.  
  58.     IniFile:      string;
  59.     procedure ClearOldButs;
  60.     procedure DeleteOldButs;
  61.     function  ReadIniFile(const INIFileName: string;
  62.                           var OptionsList: TStringList): boolean;
  63.     function  CreateIniFile(const INIFileName: string;
  64.                             OptionsList: TStringList): boolean;
  65.     procedure DynamicSpeedButtonClick(Sender: TObject);
  66.   end;
  67.  
  68. var
  69.   Form1: TForm1;
  70.  
  71. implementation
  72. const
  73.   DELTA = 2;
  74. var
  75.   pLocation :   TPoint;
  76.   BmpPath :     string;
  77.   iRow:         byte;
  78.   SearchRec :   TSearchRec;
  79.  
  80. function TForm1.CreateNewSpeedBtn(location: TPoint; size: integer;
  81.                                    BmpFile: string) : boolean;
  82. const
  83.   iCount : Integer = 0;
  84. var
  85.   GlyphBtn: TSpeedButton;
  86. begin
  87.   GlyphBtn := TSpeedButton.Create(PanelClient);
  88.   GlyphBtn.Parent := PanelClient;
  89.   with GlyphBtn do
  90.   begin
  91.     Left := location.x;
  92.     Top := location.y;
  93.     Width := size;
  94.     Height := size;
  95.     Caption := '';
  96.     TabOrder := iCount;
  97.     Tag := iCount;
  98.     OnClick := DynamicSpeedButtonClick;
  99.     if BmpFile <> '' then
  100.     try
  101.       NumGlyphs := iNumOfGlyphs;
  102.       Glyph.LoadFromFile(BmpFile);
  103.       Hint := ExtractFileName(BmpFile);
  104.       ShowHint := TRUE;
  105.       Result := TRUE;
  106.     except
  107.       MessageDlg('Error! - Unable to load ' + BmpFile + ' into button',
  108.                  mtError, [mbOK], 0);
  109.       Result := FALSE;
  110.     end;
  111.     Visible := TRUE;
  112.   end;
  113.   Inc(iCount, 1);
  114. end;
  115.  
  116. {$R *.DFM}
  117.  
  118. procedure TForm1.Initialize;
  119. begin
  120.   pLocation.x := DELTA;
  121.   pLocation.y := DELTA;
  122.   iRow := 0;
  123.   BmpPath := 'c:\delphi\images\buttons';
  124. end;
  125.  
  126. procedure TForm1.DisplayAll(Sender: TObject);
  127. begin
  128.   Screen.Cursor := CrHourGlass;
  129.   Initialize;
  130.   FormAdjustSize;
  131.   DeleteOldButs;
  132.   if not DirExists(ComboBoxDir.Text) then
  133.   begin
  134.     Screen.Cursor := CrDefault;
  135.     MessageDlg('Directory '+ComboBoxDir.Text+' does not exist',
  136.                 mtWarning, [mbOK], 0);
  137.     ComboBoxDir.Items.Delete(ComboBoxDir.ItemIndex);
  138.   end
  139.   else
  140.   begin
  141.     Display;
  142.     SpeedButtonPgDn.Enabled := TRUE;
  143.     SpeedButtonDisplay.Caption := '&Display';
  144.   end;
  145.   Screen.Cursor := CrDefault;
  146. end;
  147.  
  148. procedure TForm1.Display;
  149. var
  150.   iResult, iCount: integer;
  151. begin
  152.   iResult := findfirst(ComboBoxDir.Text + '\*.bmp', faAnyFile, SearchRec);
  153.   if iResult <> 0 then
  154.     MessageDlg( 'No files found in ' + ComboBoxDir.Text, mtError, [mbOK], 0);
  155.   iCount := 0;
  156.   while (iResult = 0) and (iCount < iSetLimit) do
  157.   begin
  158.     pLocation := Position(pLocation);
  159.     CreateNewSpeedBtn(pLocation, iButSize, ComboBoxDir.Text
  160.                                            +'\' + SearchRec.Name);
  161.     Application.ProcessMessages;
  162.     iResult := FindNext(SearchRec);
  163.     Inc(iCount, 1);
  164.   end;
  165. end;
  166.  
  167. { returns new position for a button based on the old one }
  168. function TForm1.Position(OldPosition: TPoint) : TPoint;
  169. var
  170.   iScreenWidth, iOffset: integer;
  171. begin
  172.   iOffset := PanelClient.BorderWidth + DELTA;
  173.   { calc x coord }
  174.   iScreenWidth := PanelClient.Width - 2*PanelClient.BorderWidth;
  175.   if(pLocation.x >= iScreenWidth - iButSize - 2*iOffset) or
  176.     (iRow = 0) then
  177.   begin
  178.     pLocation.x := iOffset; { start new row }
  179.     iRow := iRow + 1;
  180.   end
  181.   else
  182.     pLocation.x := OldPosition.x + iButSize;
  183.   { calculate y coord }
  184.   pLocation.y := (iRow-1)*iButSize + iOffset;
  185.   Position := pLocation;
  186. end;
  187.  
  188. procedure TForm1.FormAdjustSize;
  189. var
  190.   iNumOfButs_x, iNumOfButs_y: byte;
  191.   iCaption_y, a, b: integer;
  192. begin
  193.   iCaption_y := GetSystemMetrics(sm_cyCaption);
  194.   a := GetSystemMetrics(sm_cxFrame);
  195.   b := GetSystemMetrics(sm_cyFrame);
  196.   { adjust horizontal size }
  197.   iNumOfButs_x := (PanelClient.Width - 2*PanelClient.BorderWidth) div
  198.                   iButSize;
  199.   Width := iButSize*iNumOfButs_x + 2*(PanelClient.BorderWidth + a) + 4;
  200.   { adjust vertical size }
  201.   if iSetLimit mod iNumOfButs_x <> 0 then
  202.     iNumOfButs_y := iSetLimit div iNumOfButs_x + 1
  203.   else
  204.     iNumOfButs_y := iSetLimit div iNumOfButs_x;
  205.   Height := iNumOfButs_y*iButSize +
  206.             2*(PanelClient.BorderWidth + b) +
  207.             PanelToolBar.Height + iCaption_y + 3;
  208. end;
  209.  
  210. procedure TForm1.DeleteOldButs;
  211. var
  212.   i,n: integer;
  213. begin
  214.   n := PanelClient.ComponentCount;
  215.   for i:= n - 1 downto 0 do
  216.     (PanelClient.Components[i] as TSpeedButton).Free;
  217. end;
  218.  
  219. procedure TForm1.ClearOldButs;
  220. var
  221.   i,n: integer;
  222. begin
  223.   n := PanelClient.ComponentCount;
  224.   for i:= n - 1 downto 0 do
  225.     (PanelClient.Components[i] as TSpeedButton).Glyph := nil;
  226. end;
  227.  
  228. procedure TForm1.BitBtnClearClick(Sender: TObject);
  229. begin
  230.   ClearOldButs;
  231. end;
  232.  
  233. procedure TForm1.SpeedBtnScrollDown(Sender: TObject);
  234. var
  235.   i: integer;
  236. begin
  237.   i := NewPage(Down, SearchRec);
  238.   if i = -18 then  { hit test }
  239.     SpeedButtonPgDn.Enabled := FALSE;
  240.   SpeedButtonDisplay.Caption := '&Reset';
  241. end;
  242.  
  243. function  TForm1.NewPage(d: Direction;
  244.                          var SearchRec: TSearchRec): integer;
  245. var
  246.   i, n: integer;
  247. begin
  248.   i := 0;
  249.   n := PanelClient.ComponentCount;
  250.   Screen.Cursor := CrHourGlass;
  251.   repeat
  252.     (PanelClient.Components[i] as TSpeedButton).Glyph.LoadFromFile(
  253.                           ComboBoxDir.Text + '\' + SearchRec.Name);
  254.     (PanelClient.Components[i] as TSpeedButton).Hint :=
  255.                                                     SearchRec.Name;
  256.     Result := FindNext(SearchRec);
  257.     Inc(i, 1);
  258.   until (i > (n - 1)) or (Result = -18);
  259.   Screen.Cursor := CrDefault;
  260. end;
  261.  
  262. procedure TForm1.PanelClientResize(Sender: TObject);
  263. var
  264.   i,n: integer;
  265. begin
  266.   pLocation.x := DELTA;
  267.   pLocation.y := DELTA;
  268.   n := PanelClient.ComponentCount;
  269.   iRow := 0;
  270.   for i:= 0 to n - 1 do
  271.   begin
  272.     pLocation := Position(pLocation);
  273.     (PanelClient.Components[i] as TSpeedButton).Left := pLocation.x;
  274.     (PanelClient.Components[i] as TSpeedButton).Top := pLocation.y;
  275.   end;
  276.   PanelClient.Align := alClient;
  277.   Application.ProcessMessages;
  278.   PanelClient.Align := alNone;
  279.   FormAdjustSize;
  280.   PanelClient.Align := alClient;
  281. end;
  282.  
  283. procedure TForm1.SpeedButtonOptionsClick(Sender: TObject);
  284. begin
  285.   FormOptions := TFormOptions.Create(Self);
  286.   if FormOptions.ShowModal = mrOK then
  287.     DisplayAll(nil);
  288.   FormOptions.Free;
  289. end;
  290.  
  291. procedure TForm1.FormCreate(Sender: TObject);
  292. var
  293.   WinDir: string;
  294. begin
  295.   Initialize;
  296.   SpeedButtonPgDn.Enabled := FALSE;
  297.   ComboBoxDir.ItemIndex := 0;
  298.   iNumOfGlyphs := 2;
  299.   iSetLimit := 20;
  300.   iButSize := 45;
  301.   OptionsList := TStringList.Create; { ini file }
  302.   GetWindowsDirectory(@WinDir[1], 254);
  303.   WinDir[0] := char(StrLen(@WinDir[1]));
  304.   IniFile := WinDir + '\' + 'GlyphPro.Ini';
  305.   if not FileExists(IniFile) then
  306.     CreateIniFile(IniFile, OptionsList);
  307.   ReadIniFile(IniFile, OptionsList);
  308.   ComboBoxDir.Text := ComboBoxDir.Items.Strings[0];
  309.   DisplayAll(nil);
  310. end;
  311.  
  312. procedure TForm1.SpeedButtonAboutClick(Sender: TObject);
  313. begin
  314.   MessageDlg('Glyph Pro prodused by '#13#10 +
  315.              'Alex Tuller'#13#10 +
  316.              'CIS 102247,747'#13#10 +
  317.              'Stamford, US'#13#10#10'   Enjoy!',
  318.              mtInformation, [mbOK], 0);
  319.   ComboBoxDir.SetFocus;
  320.   ComboBoxDir.SelLength := 0;
  321. end;
  322.  
  323. function TForm1.DirExists(const S : String) : boolean;
  324. var
  325.   OldMode: Word;
  326.   OldDir: String;
  327. begin
  328.   Result := True;
  329.   GetDir(0, OldDir); { save old dir for return }
  330.   OldMode := SetErrorMode(SEM_FAILCRITICALERRORS);{if drive empty ,
  331.                                                     except}
  332.   try
  333.     try
  334.       ChDir(S);
  335.     except
  336.       on EInOutError do Result := FALSE;
  337.     end;
  338.   finally
  339.     ChDir(OldDir); { return to old dir }
  340.     SetErrorMode(OldMode); { restore old error mode }
  341.   end;
  342. end;
  343.  
  344. procedure TForm1.ComboBoxDirChange(Sender: TObject);
  345. begin
  346.   ClearOldButs;
  347.   SpeedButtonPgDn.Enabled := FALSE;
  348.   SpeedButtonDisplay.Caption := '&Display';
  349.   DisplayAll(nil);
  350. end;
  351.  
  352. procedure TForm1.ComboBoxDirKeyDown(Sender: TObject; var Key: Word;
  353.   Shift: TShiftState);
  354. begin { pgDn respond }
  355.   if (Key = 34) and (SpeedButtonPgDn.Enabled = TRUE) then
  356.   begin
  357.     Key := 29;
  358.     SpeedBtnScrollDown(nil)  { pgDown Pressed }
  359.   end
  360.   else if (Key = 33) then
  361.   begin
  362.     Key := 29;
  363.     DisplayAll(nil);         { PgUp Pressed }
  364.   end;
  365. end;
  366.  
  367. procedure TForm1.FormShow(Sender: TObject);
  368. begin
  369.   ComboBoxDir.SetFocus;
  370.   ComboBoxDir.SelLength := 0;
  371. end;
  372.  
  373. function TForm1.CreateIniFile(const INIFileName: string;
  374.                               OptionsList: TStringList): boolean;
  375. var i : integer;
  376. begin
  377.   with OptionsList do
  378.   begin
  379.     Add('MainFormLeft='+IntToStr(Form1.Left));
  380.     Add('MainFormTop='+IntToStr(Form1.Top));
  381.     Add('MainFormHeight='+IntToStr(Form1.Height));
  382.     Add('MainFormWidth='+IntToStr(Form1.Width));
  383.     Add('ButtonSize='+IntToStr(iButSize));
  384.     Add('NumberOfGlyphs='+IntToStr(iNumOfGlyphs));
  385.     Add('ButtonsPerPage='+IntToStr(iSetLimit));
  386.     Add('DelphiDir='+'C:\DELPHI');
  387.     Add('GlyphDir1=' + BmpPath);
  388.     for i := 2 to 15 do
  389.       Add('GlyphDir'+IntToStr(i)+'=');
  390.     SaveToFile(INIFileName);
  391.   end;
  392.  
  393. end;
  394.  
  395. function TForm1.UpdateIniFile(const INIFileName: string;
  396.                               OptionsList: TStringList): boolean;
  397. var
  398.   i : integer;
  399. begin
  400.   with OptionsList do
  401.   begin
  402.     Values['MainFormLeft'] := IntToStr(Form1.Left);
  403.     Values['MainFormTop'] := IntToStr(Form1.Top);
  404.     Values['MainFormHeight'] := IntToStr(Form1.Height);
  405.     Values['MainFormWidth'] := IntToStr(Form1.Width);
  406.     Values['ButtonSize'] := IntToStr(iButSize);
  407.     Values['NumberOfGlyphs'] := IntToStr(iNumOfGlyphs);
  408.     Values['ButtonsPerPage'] := IntToStr(iSetLimit);
  409.     Values['DelphiDir'] := sDelphiDir;
  410.     for i:= 1 to 15 do
  411.       Values['GlyphDir'+IntToStr(i)] :=
  412.                              ComboBoxDir.Items.Strings[i - 1];
  413.     SaveToFile(INIFileName);
  414.   end;
  415. end;
  416.  
  417. function TForm1.ReadIniFile(const INIFileName: string;
  418.                             var OptionsList: TStringList): boolean;
  419. var
  420.   i: integer;
  421.   s: string;
  422. begin
  423.   with OptionsList do
  424.   begin
  425.     LoadFromFile(INIFileName);
  426.     Form1.Left := StrToInt(Values['MainFormLeft']);
  427.     Form1.Top := StrToInt(Values['MainFormTop']);
  428.     Form1.Height := StrToInt(Values['MainFormHeight']);
  429.     Form1.Width := StrToInt(Values['MainFormWidth']);
  430.     iButSize := StrToInt(OptionsList.Values['ButtonSize']);
  431.     iNumOfGlyphs := StrToInt(OptionsList.Values['NumberOfGlyphs']);
  432.     iSetLimit := StrToInt(OptionsList.Values['ButtonsPerPage']);
  433.     sDelphiDir := OptionsList.Values['DelphiDir'];
  434.     for i := 1 to 15 do
  435.     begin
  436.       s := Values['GlyphDir'+IntToStr(i)];
  437.       if s <> '' then
  438.         ComboBoxDir.Items.Add(s);
  439.     end;
  440.     end;
  441. end;
  442.  
  443. procedure TForm1.FormDestroy(Sender: TObject);
  444. begin
  445.   UpdateIniFile(IniFile, OptionsList);
  446.   OptionsList.Free;
  447. end;
  448.  
  449. procedure TForm1.DynamicSpeedButtonClick(Sender: TObject);
  450. var
  451.   s: string;
  452. begin
  453.   s := UpperCase(ComboBoxDir.Text+ '\' +
  454.                                     (Sender as TSpeedButton).Hint);
  455.   MessageDlg(s,mtInformation,[mbOK],0);
  456.   ComboBoxDir.SetFocus;
  457.   ComboBoxDir.SelLength := 0;
  458. end;
  459.  
  460. procedure TForm1.SpeedButton2Click(Sender: TObject);
  461. begin
  462.   MessageDlg('GlyphPro supports:'#13#10+
  463.              '- Ini File'#13#10+
  464.              '- Up to 15 Search Directoris'#13#10+
  465.              '- Form Resizing'#13#10+
  466.              '- Customizable Number of Buttons per Page,'#13#10+
  467.              '  Glyphs per Button and Button Sizes'#13#10+
  468.              '- PgUp, PgDn Key'#13#10,
  469.              mtInformation, [mbOK], 0);
  470.   ComboBoxDir.SetFocus;
  471.   ComboBoxDir.SelLength := 0;
  472. end;
  473.  
  474. end.
  475.  
  476.  
  477.